package com.xjggb.future;
import java.util.concurrent.*;
public class Demo02 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        show3();
    }
    /*
    * 有返回值的使用
    * */
    public static void show3() throws ExecutionException, InterruptedException{
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("线程的名称" + Thread.currentThread().getName());
//            睡五秒钟
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello supplyAsync";
        },executorService);
        System.out.println("获取返回的值" + stringCompletableFuture.get());
    }
    public static void show2() throws ExecutionException, InterruptedException {
        /*
        * 创建线程池
        * */
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("true = " + Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },scheduledExecutorService);
        System.out.println("voidCompletableFuture.get() = " + voidCompletableFuture.get());
    }
    public static void show1() throws ExecutionException, InterruptedException {
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("true = " + Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println("voidCompletableFuture.get() = " + voidCompletableFuture.get());
        
    }

}
